package com.yxzr.youyou;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yxzr.youyou.base.BaseActivity;
import com.yxzr.youyou.bean.LoginBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.SharedPreferencesHelper;
import com.yxzr.youyou.utils.StatusBarUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {
    private TextView login_tv_forgetpwd;
    private EditText login_et_phone,login_et_pwd;
    private Button login_btn,login_btn_register;
    private SharedPreferencesHelper sp;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        StatusBarUtil.setStatusBarColor(this, R.color.colorPrimary);
        sp = new SharedPreferencesHelper(this,"logindata");
        setTitleWithBack();
        initView();
    }

    private void initView() {
        login_tv_forgetpwd = findViewById(R.id.login_tv_forgetpwd);
        login_btn_register = findViewById(R.id.login_btn_register);
        login_et_phone = findViewById(R.id.login_et_phone);
        login_et_pwd = findViewById(R.id.login_et_pwd);
        login_btn = findViewById(R.id.login_btn);

        login_tv_forgetpwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,ForgetPwdActivity.class);
                startActivity(intent);
            }
        });
        login_btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
    }

    private void login(){
        if ("".equals(login_et_phone.getText().toString().trim())){
            Toast.makeText(LoginActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("".equals(login_et_pwd.getText().toString().trim())){
            Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        RequestUtil.getInstence().API().login(login_et_phone.getText().toString().trim(),login_et_pwd.getText().toString().trim())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(LoginActivity.this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        LoginBean loginBean = gson.fromJson(t,LoginBean.class);
                        sp.put("phone",login_et_phone.getText().toString().trim());
                        sp.put("token",loginBean.getData().getUser_token());
                        sp.put("userid",loginBean.getData().getId());
                        sp.put("avatar",loginBean.getData().getHead_pic());
                        sp.put("username",loginBean.getData().getUsername());
                        finish();
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {


                    }
                });

    }
}
